package com.thoughtworks.studio.tools.cardkit;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.TreeMap;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jhotdraw.util.ResourceBundleUtil;

import com.thoughtworks.studio.tools.cardkit.parser.CardParser;
import com.thoughtworks.studio.tools.cardkit.parser.ParserException;

public class CardRepository {
	private static Logger logger = Logger.getLogger(CardManager.class);
	private CardParser cardParser;
	private String cardsroucefilename;
	private String cardstorefilename;
	private FileOutputStream cardsoutput = null;
	private Map<String, Card> cards = null;
	private Map<String, LinkedHashSet<Object>> propertyMap = null;
	private static String[] propertyVisibility = new String[] {
//			"Card Type",
//			"Release",
			"Priority",
			"Status",
//			"Story estimate"
	};
	private Map<String,CardProperty> cardProperties = new TreeMap<String,CardProperty>();

	public void addCardProperty(String name, CardProperty value){
		if(!cardProperties.containsKey(name)){
			cardProperties.put(name, value);
		}
	}
	public CardProperty getCardProperty(String name){
		if(cardProperties.containsKey(name)){
			return cardProperties.get(name);
		}
		return null;
	}
	ResourceBundleUtil labels = ResourceBundleUtil
			.getLAFBundle("com.thoughtworks.studio.tools.cardkit.gui.Labels");

	public File getPropertyImageFile(String propertyvalue) {
		String rootpath = System.getProperty("user.dir") + "/bin";
		if (!propertyvalue.startsWith("/")) {
			String imageDir;
			try {
				imageDir = labels.getString("$imageDir");
				if (!imageDir.endsWith("/")) {
					imageDir = imageDir + "/";
				}
			} catch (MissingResourceException e) {
				imageDir = "";
			}
			propertyvalue = rootpath + imageDir + propertyvalue + ".jpg";
		}
		return new File(propertyvalue);

	}

	public CardParser getCardParser() {
		return cardParser;
	}

	public static CardRepository getInstance() {
		return (CardRepository) BeanFactory.getBean("cardRepository");
	}

	public Map<String, Card> getCards() {
		if (cards == null) {
			try {
				propertyMap = cardParser.parse(cardsroucefilename);

				cards = cardParser.getCards();
			} catch (FileNotFoundException e) {
				logger.warn("can not find the file " + cardsroucefilename);
			} catch (IOException e) {
				logger.warn("can not read the file " + cardsroucefilename);
			} catch (ParserException e) {
				logger.warn("Can not parse the file " + cardsroucefilename);
				e.printStackTrace();
			}
		}
		return cards;
	}

	public void setCards(Map<String, Card> cards) {
		this.cards = cards;
	}

	public CardParser getParser() {
		return cardParser;
	}

	public void setCardParser(CardParser cardParser) {
		this.cardParser = cardParser;
	}

	public void setCardsroucefilename(String cardsroucefilename) {
		this.cardsroucefilename = cardsroucefilename;
	}

	public String getCardsroucefilename() {
		return cardsroucefilename;
	}

	public Map<String, LinkedHashSet<Object>> getPropertyMap() {
		return propertyMap;
	}

	public void setPropertyMap(Map<String, LinkedHashSet<Object>> propertyMap) {
		this.propertyMap = propertyMap;
	}

	public String[] getPropertyVisibility() {
		return propertyVisibility;
	}

	public Card getCard(String cardNum) {
		if (cards == null) {
			getCards();
		}
		return cards.get(cardNum);
	}
	
	public void saveCard() {

		try {
			if (cardsoutput == null) {
				cardsoutput = new FileOutputStream("d:\\foo.xls");
			}
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFSheet s = wb.createSheet();
			wb.setSheetName(0, "Matrix");
			
			for (short i = 0; i < 50; i++) {
				HSSFRow row = s.createRow(i);
				for (short j = 0; j < 50; j++) {
					HSSFCell cell = row.createCell(j);
					cell.setCellValue(new HSSFRichTextString("" + i + "," + j));
				}
			}
			wb.write(cardsoutput);
			cardsoutput.close();
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	public String getCardstorefilename() {
		return cardstorefilename;
	}

	public void setCardstorefilename(String cardstorefilename) {
		this.cardstorefilename = cardstorefilename;
	}
}
